<?php

namespace Auth\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\HydratingResultSet;

class UsersMapper
{
    protected $tableName = 'users';
    protected $dbAdapter;
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->dbAdapter = $adapter;
        $this->sql = new Sql($adapter);
        $this->sql->setTable($this->tableName);
    }
    
    public function saveUsers(UsersEntity $usersEntity)
    {
        $hydrator = new ClassMethods();
        $data = $hydrator->extract($usersEntity);
        if($usersEntity->getUsrId())
        {
            
        }
        else
        {
            $action = $this->sql->insert();
            unset($data['usr_id']);
            $action->values($data);
        }
        $statement = $this->sql->prepareStatementForSqlObject($action);
        try
        {
           // die(print_r($statement));
            $result = $statement->execute();
            if(!$usersEntity->getUsrId())
            {
                $usersEntity->setUsrId($result->getGeneratedValue());
            }
            
        } catch (Exception $ex) {
            die(print 'error');
            $result = NULL;
        }
        
        
        return $result;
    }
}